#####################################################################
# Appendix B and C
#####################################################################

rm(list=ls())

library(Hmisc)
library(exactRankTests)
library(ggplot2)
library(reshape2)
library(xtable)
library(stargazer)
library(sensitivitymw)
library(sensitivitymv)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)

# Read data and prepare basic output
load("~/Dropbox/Crime Brazil/06_replication/001_rematch_crime.Rdata")

#########################
# Descriptive statistics
#########################

# Before matching 

# Gen new dataset for before
d_before_descriptive = data.frame(d$v2_bairrosafe_wave1_imp, # 1
                                  d$v7a_mediatvyesorno_wave3_imp, # 2
                                  d$v9a_mediapaperyesorno_wave3_imp, # 3 
                                  d$v10c_mediainternetfreq_wave3_imp, # 4
                                  d$v16e_conversefriends_wave3_imp, # 5
                                  d$v16g_conversefamily_wave3_imp, # 6
                                  d$v20_comparetobairro_wave3_imp, # 7
                                  d$v30a_combat_crime_wave1, # 8
                                  d$v35a_attentionpres_wave2_imp, # 9
                                  d$v36a_persuadefromothersyesorno_wave3_imp, # 10
                                  d$v40a_thermmilitary_wave1_imp, # 11
                                  d$v40b_thermCUT_wave1_imp, # 12
                                  d$v40d_thermbusiness_wave1_imp, # 13 
                                  d$v41b_thermfhc_wave3_imp, # 14
                                  d$v43a_pidyesorno_wave3, # 15
                                  d$v46_pidimportant_wave1_imp, # 16
                                  d$v50_ideology_wave3_imp, # 17
                                  d$v69a_issuessocialspend_wave3_imp, # 18 
                                  d$v72b_issuesrendaminima_wave3_imp, # 19
                                  d$s1_sex_wave3, # 20
                                  d$s6_education_wave2_imp, # 21
                                  d$s7a_jobfixed_wave3_imp, # 22
                                  d$s7b_jobformalsector_wave1_imp, # 23
                                  d$s7c_jobpublicsector_wave1_imp, # 24
                                  d$s7e_jobworry_wave3_imp, # 25
                                  d$s10_age_wave3_imp, # 26
                                  d$s14c_know3fhcparty_wave3, # 27
                                  d$crime_stronghand_wave1, # 28
                                  d$deathpenalty_reducecrime_wave1, # 29
                                  d$supportdem_wave1, # 30
                                  d$deathpenalty_support_wave2, # 31
                                  d$voteforciro_wave3, # 32
                                  d$voteforlula_wave3, # 33
                                  d$voteforserra_wave3, # 34
                                  d$voteforgarotinho_wave3, # 35
                                  d$party_pmdb_wave3, # 36
                                  d$party_pfl_wave3, # 37
                                  d$party_psdb_wave3, # 38
                                  d$party_pt_wave3, # 39
                                  d$race_white_wave1, # 40
                                  d$race_mestizo_wave1, # 41
                                  d$race_black_wave1, # 42
                                  d$vote98_fhc_wave1, # 43
                                  d$vote98_lula_wave1, # 44
                                  d$religion_catholic_wave1, # 45
                                  d$religion_evangelic_wave1, # 46
                                  d$noreligion_wave1) # 47

colnames(d_before_descriptive) = c("Perceptions of safety", #mom1
                                   "Do you watch TV news?", #mom2
                                   "Do you read about politics in newspapers", #mom3
                                   "Frequency of internet usage", #mom4
                                   "Talk about politics with friends", #mom5
                                   "Talk about politics with family", #mom6
                                   "Comparison with other families", #mom7
                                   "Importance of combating crime", #mom8
                                   "Attention paid to presidential election", #mom9
                                   "Have you persuaded others to vote?", #mom10
                                   "Military feeling thermometer", #mom11
                                   "Union feeling thermometer", #mom12
                                   "Business sector feeling thermometer", #mom13
                                   "President (FHC) feeling thermometer", #mom14
                                   "Do you identify with a party?", #mom15
                                   "Importance of party when you vote", #mom16
                                   "Ideology", #mom17
                                   "Opinions about social spending", #mom18
                                   "Opinions about minimum wage", #mom19
                                   "Gender", #mom20
                                   "Education", #mom21
                                   "Stable job", #mom22
                                   "Job in the formal sector", #mom23
                                   "Job in the public sector", #mom24
                                   "Worried about losing job in the future", #mom25
                                   "Age", #mom26
                                   "Name of one presidential candidate", #mom27
                                   "Support strong-handed policies to reduce crime", #mom28
                                   "Support death penalty to reduce crime", #mom29
                                   "Support for democracy", #mom30
                                   "Support for death penalty", #mom31
                                   "Vote for Ciro", #mom32
                                   "Vote for Lula", #mom33
                                   "Vote for Serra", #mom34
                                   "Vote for Garotinho", #mom35
                                   "Do you identify with the PMDB", #mom36
                                   "Do you identify with the PFL", #mom37
                                   "Do you identify with the PSDB", #mom38
                                   "Do you identify with the PT", #mom39
                                   "White", #mom40
                                   "Pardo/Mestizo", #mom41
                                   "Black", #mom42
                                   "Voted for FHC in 1998", #mom43
                                   "Voted for Lula in 1998", #mom44
                                   "Catholic", #mom45
                                   "Evangelical", #mom46
                                   "No religion") #mom47

colnames(d_before_descriptive)

stargazer(d_before_descriptive,
          type = "text",
          colnames = FALSE,
          min.max = FALSE,
          title="Descriptive statistics before matching", 
          digits=2, 
          rownames=FALSE, 
          align=TRUE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H",
          header = FALSE, 
          no.space = TRUE)
          
# After matching 

# Gen new dataset for before
d_after_descriptive = data.frame(d_match$v2_bairrosafe_wave1_imp, # 1
                                 d_match$v7a_mediatvyesorno_wave3_imp, # 2
                                 d_match$v9a_mediapaperyesorno_wave3_imp, # 3 
                                 d_match$v10c_mediainternetfreq_wave3_imp, # 4
                                 d_match$v16e_conversefriends_wave3_imp, # 5
                                 d_match$v16g_conversefamily_wave3_imp, # 6
                                 d_match$v20_comparetobairro_wave3_imp, # 7
                                 d_match$v30a_combat_crime_wave1, # 8
                                 d_match$v35a_attentionpres_wave2_imp, # 9
                                 d_match$v36a_persuadefromothersyesorno_wave3_imp, # 10
                                 d_match$v40a_thermmilitary_wave1_imp, # 11
                                 d_match$v40b_thermCUT_wave1_imp, # 12
                                 d_match$v40d_thermbusiness_wave1_imp, # 13 
                                 d_match$v41b_thermfhc_wave3_imp, # 14
                                 d_match$v43a_pidyesorno_wave3, # 15
                                 d_match$v46_pidimportant_wave1_imp, # 16
                                 d_match$v50_ideology_wave3_imp, # 17
                                 d_match$v69a_issuessocialspend_wave3_imp, # 18 
                                 d_match$v72b_issuesrendaminima_wave3_imp, # 19
                                 d_match$s1_sex_wave3, # 20
                                 d_match$s6_education_wave2_imp, # 21
                                 d_match$s7a_jobfixed_wave3_imp, # 22
                                 d_match$s7b_jobformalsector_wave1_imp, # 23
                                 d_match$s7c_jobpublicsector_wave1_imp, # 24
                                 d_match$s7e_jobworry_wave3_imp, # 25
                                 d_match$s10_age_wave3_imp, # 26
                                 d_match$s14c_know3fhcparty_wave3, # 27
                                 d_match$crime_stronghand_wave1, # 28
                                 d_match$deathpenalty_reducecrime_wave1, # 29
                                 d_match$supportdem_wave1, # 30
                                 d_match$deathpenalty_support_wave2, # 31
                                 d_match$voteforciro_wave3, # 32
                                 d_match$voteforlula_wave3, # 33
                                 d_match$voteforserra_wave3, # 34
                                 d_match$voteforgarotinho_wave3, # 35
                                 d_match$party_pmdb_wave3, # 36
                                 d_match$party_pfl_wave3, # 37
                                 d_match$party_psdb_wave3, # 38
                                 d_match$party_pt_wave3, # 39
                                 d_match$race_white_wave1, # 40
                                 d_match$race_mestizo_wave1, # 41
                                 d_match$race_black_wave1, # 42
                                 d_match$vote98_fhc_wave1, # 43
                                 d_match$vote98_lula_wave1, # 44
                                 d_match$religion_catholic_wave1, # 45
                                 d_match$religion_evangelic_wave1, # 46
                                 d_match$noreligion_wave1) # 47

colnames(d_after_descriptive) = c("Perceptions of safety", #mom1
                                  "Do you watch TV news?", #mom2
                                  "Do you read about politics in newspapers", #mom3
                                  "Frequency of internet usage", #mom4
                                  "Talk about politics with friends", #mom5
                                  "Talk about politics with family", #mom6
                                  "Comparison with other families", #mom7
                                  "Importance of combating crime", #mom8
                                  "Attention paid to presidential election", #mom9
                                  "Have you persuaded others to vote?", #mom10
                                  "Military feeling thermometer", #mom11
                                  "Union feeling thermometer", #mom12
                                  "Business sector feeling thermometer", #mom13
                                  "President (FHC) feeling thermometer", #mom14
                                  "Do you identify with a party?", #mom15
                                  "Importance of party when you vote", #mom16
                                  "Ideology", #mom17
                                  "Opinions about social spending", #mom18
                                  "Opinions about minimum wage", #mom19
                                  "Gender", #mom20
                                  "Education", #mom21
                                  "Stable job", #mom22
                                  "Job in the formal sector", #mom23
                                  "Job in the public sector", #mom24
                                  "Worried about losing job in the future", #mom25
                                  "Age", #mom26
                                  "Name of one presidential candidate", #mom27
                                  "Support strong-handed policies to reduce crime", #mom28
                                  "Support death penalty to reduce crime", #mom29
                                  "Support for democracy", #mom30
                                  "Support for death penalty", #mom31
                                  "Vote for Ciro", #mom32
                                  "Vote for Lula", #mom33
                                  "Vote for Serra", #mom34
                                  "Vote for Garotinho", #mom35
                                  "Do you identify with the PMDB", #mom36
                                  "Do you identify with the PFL", #mom37
                                  "Do you identify with the PSDB", #mom38
                                  "Do you identify with the PT", #mom39
                                  "White", #mom40
                                  "Pardo/Mestizo", #mom41
                                  "Black", #mom42
                                  "Voted for FHC in 1998", #mom43
                                  "Voted for Lula in 1998", #mom44
                                  "Catholic", #mom45
                                  "Evangelical", #mom46
                                  "No religion") #mom47

colnames(d_after_descriptive)

stargazer(d_after_descriptive,
          type = "text",
          colnames = FALSE,
          min.max = FALSE,
          title="Descriptive statistics after matching", 
          digits=2, 
          rownames=FALSE, 
          align=TRUE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H",
          header = FALSE, 
          no.space = TRUE)
          